package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Dao {
	
	static final public int DAO_DBTYPE_OBDC = 0;
	static final public int DAO_DBTYPE_ORACLE = 1;
	static final public int DAO_DBTYPE_DB2 = 2;
	static final public int DAO_DBTYPE_MYSQL = 3;
	static final public int DAO_DBTYPE_SQLSERVER = 4;
	static final public int DAO_DBTYPE_ACCESS = 5;
	
	private Connection conn = null;
	
	private PreparedStatement prst = null;
	
	public Dao(int db) {
		super();
		try {
			switch (db) {
			case DAO_DBTYPE_DB2: {
				Class.forName("com.ibm.db2.jcc.DB2Driver");
				Connection conn = DriverManager.getConnection(
						"jdbc:db2://172.16.4.33:60000/scdb", "scread", "sc09");
				setConn(conn);
				break;
			}
			default:
				throw new RuntimeException("THIS METHOD MUST HAVE A DAO_DBTYPE VALUE SET!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			if (getConn() != null) {
				getConn().close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	public PreparedStatement getPrst() {
		return prst;
	}
	
	public PreparedStatement getPrst(String sql) {
		if (getConn() != null) {
			try {
				setPrst(getConn().prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY));
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return getPrst();
	}

	public void setPrst(PreparedStatement prst) {
		this.prst = prst;
	}
	
}
